package com.junbao.module.mine.main.viewmodel;

import android.content.Context;
import android.content.Intent;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Toast;

import androidx.databinding.ObservableField;

import com.junbao.module.mine.login.view.LoginActivity;
import com.junbao.module.mine.main.model.User;


/**
 * author : Majunbao
 * github : https://github.com/MaJunBaox
 * time   : 2022/8/21 4:23 下午
 * desc   :
 */
public class MainViewModel {

    /**
     * 上下文
     * @param context
     */
    private Context mContext;
    /**
     * 用户名
     */
    private String userName;
    /**
     * 密码
     */
    private String pwd;

    public MainViewModel(Context context) {
        this.mContext = context;
    }


    /**
     * 获取用户输入的账号方法
     * @return
     */
    public TextWatcher userNameChangeListener(){
        return new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                userName = charSequence.toString();
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        };
    }

    /**
     * 获取用户输入的密码方法
     * @return
     */
    public TextWatcher pwdChangeListener(){
        return new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                pwd = charSequence.toString();
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        };
    }

    public void login(View view){
//        if (!TextUtils.isEmpty(userName) &&!TextUtils.isEmpty(pwd)  ){
//            //模拟判断数据
//            if (userName.equals("majunbao") && pwd.equals("123")){
//                Toast.makeText(mContext, "登陆成功", Toast.LENGTH_SHORT).show();
//            }else {
//                Toast.makeText(mContext, "登陆失败", Toast.LENGTH_SHORT).show();
//            }
//        }else {
//            Toast.makeText(mContext, "用户明和密码都不能为空", Toast.LENGTH_SHORT).show();
//        }

        mContext.startActivity(new Intent(mContext , LoginActivity.class));
    }

    public ObservableField<User> userFiled = new ObservableField<>();

    public void getUserInfo(){
        User user = new User("majunbao","123");
        userFiled.set(user);
    }

}
